﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Collections.Generic;
using Microsoft.Practices.Web.UI.WebControls;
using Microsoft.Practices.CompositeWeb.Web.UI;

namespace Locator.Products.Views
{
    public partial class UsersProducts : UserControl, IUsersProductsView
    {
        private UsersProductsPresenter presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.presenter.OnViewInitialized();
            }

            this.presenter.OnViewLoaded();
        }

        [CreateNew]
        public UsersProductsPresenter Presenter
        {
            get { return this.presenter; }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this.presenter = value;
                this.presenter.View = this;
            }
        }

        public void SetProducts(List<Locator.Products.UserProduct> products)
        {
            this.ProductsDataSource.DataSource = products;
        }

        protected void ProductsDataSource_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            this.presenter.OnProductDeleted((UserProduct)e.Instance);
        }

        protected void ProductSearchLinkButton_Click(object sender, EventArgs e)
        {
            this.presenter.OnProductSearchClicked();
        }
    }
}

